import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_module/model/service_provider_audit_state.dart';

class ServiceProviderEditFooter extends StatefulWidget {
  ServiceProviderEditFooter(
      {Key? key, required this.state, this.reasonRejection, this.onAction})
      : super(key: key);

  final ServiceProviderAuditState state;
  final String? reasonRejection;
  final void Function(ServiceProviderAuditState state)? onAction;

  @override
  _ServiceProviderEditFooterState createState() =>
      _ServiceProviderEditFooterState(state);
}

class _ServiceProviderEditFooterState extends State<ServiceProviderEditFooter> {
  _ServiceProviderEditFooterState(this._state);

  ServiceProviderAuditState _state;

  /// 是否显示拒绝原因
  bool get _showReason {
    final text = widget.reasonRejection ?? '';
    return _state == ServiceProviderAuditState.auditReject && text.isNotEmpty;
  }

  /// 拒绝原因的组件
  List<Widget> get _reasonWidgets {
    if (_showReason) {
      return [
        Container(
          padding: EdgeInsets.symmetric(vertical: 8.0, horizontal: 14.0),
          color: Color(0xfffdf0da),
          child: Text(
            widget.reasonRejection ?? '',
            style: TextStyle(color: Color(0xffffa200), fontSize: 10.0),
          ),
        )
      ];
    }
    return [];
  }

  /// 操作相关的组件
  Widget get _actionWidget {
    switch (_state) {
      case ServiceProviderAuditState.prepareSubmission:
        return TextButton(
          style: TextButton.styleFrom(
            splashFactory: NoSplash.splashFactory,
            backgroundColor: Color(0xffef5d5e),
            shape: const RoundedRectangleBorder(),
          ),
          onPressed: () {
            if (widget.onAction != null) {
              widget.onAction!(_state);
            }
          },
          child: Text(
            '提交',
            style: TextStyle(
              color: Color(0xffffffff),
              fontSize: 14.0,
              fontWeight: FontWeight.w500,
            ),
          ),
        );
      case ServiceProviderAuditState.pendingAudit:
        return TextButton(
          style: TextButton.styleFrom(
            backgroundColor: Color(0xffe5e5e5),
            shape: const RoundedRectangleBorder(),
          ),
          onPressed: null,
          child: Text(
            '审核中',
            style: TextStyle(
              color: Color(0xffffffff),
              fontSize: 14.0,
              fontWeight: FontWeight.w500,
            ),
          ),
        );
      case ServiceProviderAuditState.auditReject:
        return Row(
          children: [
            Container(
              child: TextButton(
                style: TextButton.styleFrom(
                  backgroundColor: Colors.white,
                  shape: const RoundedRectangleBorder(),
                ),
                onPressed: null,
                child: Text(
                  '审核不通过',
                  style: TextStyle(
                    color: Color(0xff888888),
                    fontSize: 14.0,
                    fontWeight: FontWeight.w500,
                  ),
                ),
              ),
              width: 130,
              height: double.infinity,
            ),
            Expanded(
              child: Container(
                height: double.infinity,
                child: TextButton(
                  style: TextButton.styleFrom(
                    splashFactory: NoSplash.splashFactory,
                    backgroundColor: Color(0xffef5d5e),
                    shape: const RoundedRectangleBorder(),
                  ),
                  onPressed: () {
                    setState(() {
                      _state = ServiceProviderAuditState.prepareSubmission;
                    });
                  },
                  child: Text(
                    '修改',
                    style: TextStyle(
                      color: Color(0xffffffff),
                      fontSize: 14.0,
                      fontWeight: FontWeight.w500,
                    ),
                  ),
                ),
              ),
            )
          ],
        );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: double.infinity,
      child: Stack(
        children: [
          Column(
            children: [
              ..._reasonWidgets,
              Container(
                child: _actionWidget,
                width: double.infinity,
                height: 50.0,
              ),
            ],
          ),
          if (_showReason)
            Positioned(
              left: 62.0,
              bottom: 45.0,
              child: Image.asset(
                  'images/service_provider/service_provider_refuse_arrow.png'),
              width: 6.0,
              height: 5.0,
            ),
        ],
      ),
    );
  }
}
